import type {
  BaseProps,
  dateProps,
  inputProps,
  selectProps
} from "@/components/AuForm/type";
import { GetCharNum } from "@/utils/tool";

export const ENABLE_STATUS_LIST = [
  {
    label: "business_manage.table.all_status",
    value: ""
  },
  {
    label: "business_manage.table.in_use",
    value: "1"
  },
  {
    label: "business_manage.table.disabled",
    value: "0"
  }
];

export const operateNatureList = [
  {
    label: "business_manage.search.chain",
    value: "连锁"
  },
  {
    label: "business_manage.search.personal",
    value: "个人"
  }
];

const OPERATE_NATURE_LIST = [
  {
    label: "business_manage.search.all_nature",
    value: ""
  },
  ...operateNatureList
];

type FormListProp = [
  BaseProps,
  inputProps,
  selectProps,
  selectProps,
  inputProps,
  BaseProps,
  inputProps,
  inputProps,
  dateProps,
  inputProps,
  inputProps
];

export const SEARCH_FORM_LIST = [
  {
    type: "date",
    label: "business_manage.search.register_time",
    key: "time",
    inType: "datetimerange",
    span: 8,
    rangeSeparator: "至",
    startPlaceholder: "business_manage.search.start_time",
    endPlaceholder: "business_manage.search.end_time",
    valueFormat: "YYYY-MM-DD HH:mm:ss"
  },
  // {
  //   type: "select",
  //   key: "enable_status",
  //   label: "business_manage.table.status",
  //   placeholder: "请选择状态",
  //   options: ENABLE_STATUS_LIST,
  //   emptyValues: [null, undefined],
  //   labelKey: "label",
  //   valueKey: "value",
  //   defaultValue: ""
  // },
  {
    type: "select",
    key: "operate_nature",
    label: "business_manage.search.nature",
    placeholder: "business_manage.search.nature_placeholder",
    options: OPERATE_NATURE_LIST,
    emptyValues: [null, undefined],
    labelKey: "label",
    valueKey: "value",
    defaultValue: ""
  }
];

export const FORM_LIST = (t: (key: string) => string): FormListProp => [
  {
    type: "base",
    key: "logo",
    label: "business_manage.table.logo",
    slot: true
  },
  {
    type: "input",
    key: "brand",
    label: "business_manage.form.brand",
    placeholder: "business_manage.form.brand_placeholder",
    required: true,
    rules: [
      {
        validator: (rule: any, value: string, callback: Function) => {
          if (value !== undefined && value !== "") {
            if (GetCharNum(value) < 2) {
              callback(t("business_manage.form.brand_min"));
            } else if (GetCharNum(value) > 20) {
              callback(t("business_manage.form.brand_max"));
            } else {
              callback();
            }
          } else {
            callback(t("business_manage.form.brand_required"));
          }
        },
        trigger: "blur"
      }
    ]
  },
  {
    type: "select",
    key: "province",
    label: "business_manage.form.province",
    placeholder: "business_manage.form.province_placeholder",
    valueKey: "value",
    labelKey: "label",
    required: true,
    filterable: true,
    options: []
  },
  {
    type: "select",
    key: "city",
    label: "business_manage.form.city",
    placeholder: "business_manage.form.city_placeholder",
    valueKey: "value",
    labelKey: "label",
    required: true,
    filterable: true,
    options: []
  },
  {
    type: "input",
    key: "detail_address",
    label: "business_manage.form.detail_address",
    required: true,
    placeholder: "business_manage.form.detail_address_placeholder",
    rules: [
      {
        validator: (rule: any, value: string, callback: Function) => {
          if (value !== undefined && value !== "") {
            if (GetCharNum(value) < 2) {
              callback(t("business_manage.form.detail_address_min"));
            } else if (GetCharNum(value) > 200) {
              callback(t("business_manage.form.detail_address_max"));
            } else {
              callback();
            }
          } else {
            callback(t("business_manage.form.detail_address_required"));
          }
        },
        trigger: "blur"
      }
    ]
  },
  {
    type: "base",
    key: "operate_nature",
    label: "business_manage.form.operate_nature"
  },
  {
    type: "input",
    key: "operator",
    label: "business_manage.form.operator",
    required: true,
    placeholder: "business_manage.form.operator_placeholder",
    rules: [
      {
        validator: (rule: any, value: string, callback: Function) => {
          if (value !== undefined && value !== "") {
            if (GetCharNum(value) < 2) {
              callback(t("business_manage.form.operator_min"));
            } else if (GetCharNum(value) > 100) {
              callback(t("business_manage.form.operator_max"));
            } else {
              callback();
            }
          } else {
            callback(t("business_manage.form.operator_required"));
          }
        },
        trigger: "blur"
      }
    ]
  },
  {
    type: "input",
    key: "phone",
    label: "business_manage.form.phone",
    maxLength: 20,
    placeholder: "business_manage.form.phone_placeholder",
    required: true,
    rules: [
      {
        validator: (rule: any, value: string, callback: Function) => {
          if (value !== undefined && value !== "") {
            const testPhone = /^\+?[1-9]\d{1,14}$/;
            if (!testPhone.test(value)) {
              callback(t("business_manage.form.phone_format"));
            } else {
              callback();
            }
          } else {
            callback(t("business_manage.form.phone_required"));
          }
        },
        trigger: "blur"
      }
    ]
  },
  {
    type: "date",
    label: "business_manage.form.expire_time",
    key: "expire_time",
    inType: "datetime",
    placeholder: "business_manage.form.expire_time_placeholder",
    valueFormat: "YYYY-MM-DD HH:mm:ss",
    disabledDate: (time: any) => {
      return time.getTime() < new Date().getTime();
    },
    rules: [
      {
        validator: (rule: any, value: string, callback: Function) => {
          // 创建 2038-01-01 的日期
          const maxDate = new Date(2038, 0, 1); // 2038年1月1日

          // 如果时间超过了 2038-01-01，则禁用
          const curVal = new Date(value);
          if (value && curVal.getTime() > maxDate.getTime()) {
            callback(t("business_manage.form.expire_time_max"));
          } else {
            callback();
          }
        },
        trigger: "change"
      }
    ]
  },
  {
    type: "input",
    key: "account",
    label: "business_manage.form.account",
    required: true,
    placeholder: "business_manage.form.account_placeholder",
    rules: [
      {
        validator: (rule: any, value: string, callback: Function) => {
          if (value !== undefined && value !== "") {
            if (GetCharNum(value) < 2) {
              callback(t("business_manage.form.account_min"));
            } else if (GetCharNum(value) > 20) {
              callback(t("business_manage.form.account_max"));
            } else {
              callback();
            }
          } else {
            callback(t("business_manage.form.account_required"));
          }
        },
        trigger: "blur"
      }
    ]
  },
  {
    type: "input",
    key: "password",
    label: "business_manage.form.password",
    required: true,
    placeholder: "business_manage.form.password_placeholder",
    inType: "password",
    showPassword: true,
    rules: [
      {
        validator: (rule: any, value: string, callback: Function) => {
          if (value === "") {
            callback(new Error(t("business_manage.form.password_required")));
          } else if (
            !RegExp(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,15}$/).test(value)
          ) {
            callback(new Error(t("business_manage.form.password_format")));
          } else {
            callback();
          }
        },
        trigger: "blur"
      }
    ]
  }
];
